package com.shanyue.redwine.wxapi;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.shanyue.redwine.R;
import com.shanyue.redwine.activity.ActivityBase;
import com.shanyue.redwine.utils.Constants;
import com.shanyue.redwine.utils.SPUtils;
import com.shanyue.redwine.utils.SweetAlertDialogFactory;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import cn.pedant.SweetAlert.SweetAlertDialog;

public class WXPayEntryActivity extends ActivityBase implements IWXAPIEventHandler {
    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);

        api = WXAPIFactory.createWXAPI(this, Constants.PAY_WEIXIN_APPID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @SuppressLint("StringFormatInvalid")
    @Override
    public void onResp(BaseResp resp) {
        //		Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            final int payType = (int) SPUtils.get(getContext(), Constants.SP_WX_PAY_TYPE, -1);
            if (resp.errCode == 0) {
                Toast.makeText(WXPayEntryActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
                //                SweetAlertDialogFactory.build(WXPayEntryActivity.this, SweetAlertDialog
                // .SUCCESS_TYPE, false)
                //                        .setContentText("支付成功").setConfirmClickListener(sweetAlertDialog -> {
                //                    sweetAlertDialog.dismiss();
                Intent intent = new Intent();
                switch (payType) {
//                    case Constants.WEIXIN_PAY_GOODS_ORDER:
//                        AppTaskManager.getAppManager().finishActivity(ActivityPay.class);
//                        intent.setClass(getContext(), ActivityOrder.class);
//                        intent.putExtra(Constants.KEY_DATA, ActivityOrder.ORDER_STATE_UN_SEND);//跳转到订单列表的已支付标签页
//                        startActivity(intent);
//                        break;
//                    case Constants.WEIXIN_PAY_RECHARGE://到余额页面
//                        intent.setClass(getContext(), ActivityCapital.class);
//                        intent.putExtra(Constants.KEY_DATA, 3);//只可以为任意值 只要有这个key就认为是支付成功了
//                        startActivity(intent);
//                        break;

                }
                finish();
                //                }).show();

            } else if (resp.errCode == -1) {
                SweetAlertDialogFactory.build(WXPayEntryActivity.this, SweetAlertDialog.ERROR_TYPE, false)
                        .setContentText("支付失败").setConfirmClickListener(sweetAlertDialog -> {
                    sweetAlertDialog.dismiss();
                    Intent intent = new Intent();
                    switch (payType) {
                        case Constants.WEIXIN_PAY_GOODS_ORDER:
//                            AppTaskManager.getAppManager().finishActivity(ActivityPay.class);
//                            intent.setClass(getContext(), ActivityOrder.class);
//                            intent.putExtra(Constants.KEY_DATA, ActivityOrder.ORDER_STATE_UN_PAY);//跳转到订单列表的已支付标签页
//                            startActivity(intent);
                            break;
                        case Constants.WEIXIN_PAY_RECHARGE://不用任何处理 直接finish返回 重新充值或者取消
                            break;

                    }
                    finish();
                }).show();
            } else {//-2 用户取消了支付
                SweetAlertDialogFactory.build(WXPayEntryActivity.this, SweetAlertDialog.ERROR_TYPE, false)
                        .setContentText("支付已取消").setConfirmClickListener(sweetAlertDialog -> {
                    sweetAlertDialog.dismiss();
                    Intent intent = new Intent();
                    switch (payType) {
                        case Constants.WEIXIN_PAY_GOODS_ORDER:
//                            AppTaskManager.getAppManager().finishActivity(ActivityPay.class);
//                            intent.setClass(getContext(), ActivityOrder.class);
//                            intent.putExtra(Constants.KEY_DATA, ActivityOrder.ORDER_STATE_UN_PAY);//跳转到订单列表的已支付标签页
//                            startActivity(intent);
                            break;
                        case Constants.WEIXIN_PAY_RECHARGE://不用任何处理 直接finish返回 重新充值或者取消
                            break;

                    }
                    finish();
                }).show();
            }

        }
    }

    private Context getContext() {
        return this;
    }
}